package helpers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class NumberHelper {
	private static List<String> alphabets = new ArrayList<String>(Arrays.asList(
			"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", 
			"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" 
			));
	
	public static int getRandomNumberFrom(int min, int max) {
        Random foo = new Random();
        int randomNumber = foo.nextInt((max + 1) - min) + min;

        return randomNumber;

    }
	
	public static String IntToString(int Int) {
		String str = "";
		if (Int < 27 && Int > 0) {
			str = alphabets.get(Int-1);
		} else if (Int <= 0) {
			str = alphabets.get(alphabets.size() - 1);
		} else {
			if (Int%26 != 0) {
				str = IntToString(Int/26) + IntToString(Int%26);
			} else {
				str = IntToString(Int/26 - 1) + IntToString(Int%26);
			}
		}
		
		return str;
	}

/**
	A: 1 = 26*0 + 1
	B: 2 = 26*0 + 2
	AA: 27 = 26*1 + 1
	AB: 28 = 26*1 + 2
	BA: 53 = 26*2 + 1
	AAA: 703 = 676 + 26 + 1 = 26*26*1 + 26*1 + 1
	AAB: 704 = 676 + 26 + 2 = 26*26*1 + 26*1 + 2
	ABA: 729 = 676 + 52 + 1 = 26*26*1 + 26*2 + 1
	BAA: 1379 = 1352 + 26 + 1 = 26*26*2 + 26*1 + 1
*/
	
	public static int StringToInt(String str) {
		int Int = 0;
		
		for (int i = 1; i < str.length() + 1; i++) {
			String ch = str.substring(i-1, i);
			int idx = getIndexOfList(ch);
			if (i == str.length()) {
				Int += idx;
			} else {
				Int += (int)Math.pow(26, str.length()-i)*idx;
			}
		}
		
		return Int;
	}
	
	private static int getIndexOfList(String str) {
		int idx = 1;
		for (int i = 0; i < alphabets.size(); i++) {
			if (str.equalsIgnoreCase(alphabets.get(i))) {
				idx = i + 1;
				break;
			}
		}
		return idx;
	}
	
	public static void main(String[] args) {
		//System.out.println(27/26);
		for (int i = 1; i < 2000; i++) {
			String str = IntToString(i);
			int Int = StringToInt(str);
			System.out.println("i:" + i + " => " + str + " => " + Int);
		}
		
		
		/*int a = 2;
		int b = 5;
		System.out.println((int) Math.pow(a, b));*/
		System.out.println("End --------------");
	}
}
